package utils

import "github.com/dgrijalva/jwt-go"

type Jwt struct {
	SignKey []byte
}

func NewJwt(SignKey []byte) *Jwt {
	return &Jwt{SignKey: SignKey}
}

func (this *Jwt) CreateToken(claims jwt.Claims) (string, error) {
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	return token.SignedString(this.SignKey)
}

func (this *Jwt) ParseToken(tokenString string) (*jwt.Token, error) {
	token, err := jwt.Parse(tokenString, func(token *jwt.Token) (i interface{}, e error) {
		return this.SignKey, nil
	})
	if err != nil {
		return token, err
	}
	return token, nil
}
